//
//  ShoppingCarViewController.m
//  HaoDeCaiFu
//
//  Created by 物联信息中心 on 2018/1/3.
//  Copyright © 2018年 郑开宏. All rights reserved.
//

#import "ShoppingCarViewController.h"
#import "ShoppingCarTableViewCell.h"
#import "chooseDeleteOrBuyView.h"
#import "jijinAndGroupDetailModel.h"
#import "ShoppingListModel.h"
#import "Allhelp.h"
#import "ShoppingBuyViewController.h"
#import "HDCFFundListVc.h"//基金列表vc

@interface ShoppingCarViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    jijinAndGroupDetailModel * shopModel;
    NSString * textFieldStr;
    
    //NSMutableArray * investMoneyArr;//投资金额的数组
    NSMutableArray * fundTypeArr;//基金类型数组
    NSInteger indexs;//编辑的第几个cell的金额
    //NSMutableArray * selectIndexArr;//选中的索引数组
    
    NSMutableArray * shopArrays;//购买成功时页面带回来的购买成功的数组
}
@property(nonatomic,strong)UIView*kongbaiView;
@property(nonatomic,strong)UITableView*tableView;
@property(nonatomic,strong)chooseDeleteOrBuyView*deleteViewView;
@property(nonatomic,assign)BOOL isAllChoose;
@property(nonatomic,strong)NSMutableArray*jijinDataArray;
@property(nonatomic,strong)NSMutableArray*haveSelectArray;

@end

@implementation ShoppingCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.view addSubview:self.kongbaiView];
    self.jijinDataArray=[NSMutableArray array];
    self.haveSelectArray=[NSMutableArray array];
    fundTypeArr = [NSMutableArray array];
    //investMoneyArr = [NSMutableArray array];
    //selectIndexArr = [NSMutableArray array];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ShoppingCarTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShoppingCarTableViewCell"];
    
    
    UIView*chooseView=[[UIView alloc]initWithFrame:CGRectMake(0, screenHeight-50-KNavHeight-49, screenWidth, 50)];
    
    self.deleteViewView =[chooseDeleteOrBuyView fixTrowSetView];
    [_deleteViewView.chooseBtn addTarget:self action:@selector(chooseAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_deleteViewView.deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_deleteViewView.buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _deleteViewView.frame=CGRectMake(0, 0, screenWidth, 50);
    [chooseView addSubview:_deleteViewView];
    [self.view addSubview:chooseView];
}
-(void)viewWillAppear:(BOOL)animated
{
    //获得购物车列表数据
    if (kToken) {
    [self initData];
    
    
    //应该清空已经选择的数组
    [self.haveSelectArray removeAllObjects];
    
    //让购买按钮状态显示正常
   
        self.deleteViewView.buyBtn.backgroundColor=[UIColor lightGrayColor];
        [self.deleteViewView.buyBtn setTitle:[NSString stringWithFormat:@"购买"]  forState:UIControlStateNormal];
    }
    
}
#pragma mark -
#pragma mark RequestData
//获得购物车列表数据
-(void)initData
{
    [SVProgressHUD show];
    NSString * urlStr = @"api/ShoppingCart/Get";
//    NSMutableDictionary *paramet = [NSMutableDictionary dictionary];
//    paramet[@"PageIndex"] = kAcount;
//    paramet[@"PageSize"] = @"10";
    
    [HttpRequest sendSelfGetRequestWithURL:urlStr WithParamets:nil success:^(id result) {
        LLLog(@"%@",result);
        NSString *code = [result[@"code"]stringValue];
        if ([code isEqualToString:@"1"]) {
            [SVProgressHUD dismiss];
            if (self.jijinDataArray.count > 0 ) {
                [self.jijinDataArray removeAllObjects];
            }
            NSArray * resultArr = result[@"result"];
            
            if (!resultArr.count||resultArr<=0) {
                [self.view addSubview:self.kongbaiView];
                
            }
            else
            {
                [self.kongbaiView removeFromSuperview];
                for (int i = 0; i < resultArr.count; i++) {
                    NSDictionary * dict = resultArr[i];
                    
                    shopModel = [jijinAndGroupDetailModel mj_objectWithKeyValues:dict];
                    shopModel.isChoose=NO;
                    [self.jijinDataArray addObject:shopModel];
                    //保存投资金额到数组内
                    //                if ([shopModel.Amount intValue] <= 0 ) {
                    //                    [investMoneyArr addObject:@""];
                    //                }
                    //                else
                    //                {
                    //                    [investMoneyArr addObject:shopModel.Amount];
                    //                }
                    
                }
                if (shopArrays.count > 0) {
                    [self clearShopWhenBuySeccuss:shopArrays];
                }
                [self getDataOfBasic];
                
                [self.tableView reloadData];
            }
            
            
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        LLLog(@"%@",errer);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
//获得购物车编辑
-(void)editShop:(jijinAndGroupDetailModel *)model
{
    [self.view endEditing:YES];
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"UserId"] = @"";
    paramets[@"FundCode"] = model.fundCode;
    paramets[@"FundName"] = model.fundName;
    paramets[@"Amount"] = textFieldStr;
    paramets[@"UpdateTime"] = [Allhelp geyTime];
    paramets[@"Id"] = [NSString stringWithFormat:@"%ld",(long)model.Id];
    //LLLog(@"%@",paramets);
    NSString *jsonStr = [self dictionaryToJson:paramets];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/ShoppingCart/Edit" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        [SVProgressHUD dismiss];
        //LLLog(@"%@",result);
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {
            //[investMoneyArr replaceObjectAtIndex:indexs withObject:textFieldStr];
            model.Amount = textFieldStr;
            //[SVProgressHUD showSuccessWithStatus:@"金额编辑成功"];
            //[SVProgressHUD dismissWithDelay:1.0];
            //[self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        LLLog(@"error:%@",errer);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
//删除购物车
-(void)deleteShop
{
    [SVProgressHUD show];
    NSMutableArray * bodyArr = [NSMutableArray array];
    for (int i = 0; i < self.haveSelectArray.count; i++) {
       
       jijinAndGroupDetailModel* jijinModel = self.haveSelectArray[i];
        [bodyArr addObject:jijinModel.fundCode];
    }
    NSString * jsonStr = [self arrayToJSONString:bodyArr];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/ShoppingCart/Delete" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        //LLLog(@"%@",result);
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {

            
           
           
            for (jijinAndGroupDetailModel * jijinModel in self.jijinDataArray.reverseObjectEnumerator) {
                
              
                for (jijinAndGroupDetailModel * jijinModel11 in self.haveSelectArray) {
                    
                    if ([jijinModel11.Id isEqualToString:jijinModel.Id])
                    {
                        [self.jijinDataArray removeObject:jijinModel];
                       
                        
                            self.deleteViewView.buyBtn.backgroundColor=[UIColor lightGrayColor];
                            [self.deleteViewView.buyBtn setTitle:[NSString stringWithFormat:@"购买"]  forState:UIControlStateNormal];
                       
                        
                    }
                }
            }
            
            
            
            
            
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            [self.haveSelectArray removeAllObjects];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        //LLLog(@"error:%@",errer);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
//购买成功删除购物车（调用的还是删除购物车的接口）
-(void)clearShopWhenBuySeccuss:(NSArray *)shopArray
{
    [SVProgressHUD show];
    NSMutableArray * bodyArr = [NSMutableArray array];
    for (int i = 0; i < shopArray.count; i++) {
        jijinAndGroupDetailModel * jijinModel = [[jijinAndGroupDetailModel alloc]init];
        jijinModel = shopArray[i];
        [bodyArr addObject:jijinModel.fundCode];
    }
    NSString * jsonStr = [self arrayToJSONString:bodyArr];
    [HttpRequest sendSelfPostRequestWithURL:@"/api/ShoppingCart/Delete" WithParamets:jsonStr WithParamet:nil success:^(id result) {
        //LLLog(@"%@",result);
        NSDictionary*dic=(NSDictionary*)result;
        NSNumber*number=[dic objectForKey:@"code"];
        if ([number isEqual:@1]) {
            [shopArrays removeAllObjects];
            
            for (jijinAndGroupDetailModel * jijinModel in self.jijinDataArray.reverseObjectEnumerator) {
                for (NSString * fundcodeStr in bodyArr) {
                    if ([fundcodeStr isEqualToString:jijinModel.fundCode])
                    {
                        [self.jijinDataArray removeObject:jijinModel];
                    }
                }
            }
            [SVProgressHUD showSuccessWithStatus:@"删除成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"msg"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        //LLLog(@"error:%@",errer);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
//批量获取基金信息
- (void)getDataOfBasic{
    [SVProgressHUD show];
    NSMutableArray * fundcodeArray = [NSMutableArray array];
    NSDictionary *param = [NSDictionary dictionary];
    if (self.jijinDataArray.count <= 0) {
        return;
    }
    for (int i = 0; i < self.jijinDataArray.count; i++) {
        jijinAndGroupDetailModel * model = self.jijinDataArray[i];
        [fundcodeArray addObject:model.fundCode];
    }
    NSString *string = [fundcodeArray componentsJoinedByString:@","];
    param = @{@"fundCodes":string};
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[KparametTwo] = @"/public/product/batch-get-fund-info";
    dict[KparametOne] = [self dictionaryToJson:param];
    [HttpRequest sendGetRequestWithURL:nil WithParamets:dict success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD dismiss];
            //LLLog(@"%@",result);
            NSDictionary * dataDic = result[@"data"];
            NSArray * dataArray = dataDic[@"data"];
            if (fundTypeArr.count > 0) {
                [fundTypeArr removeAllObjects];
            }
            for (NSDictionary * dic in dataArray) {
                [fundTypeArr addObject:dic[@"fundType"]];
            }
            
            [self.tableView  reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}
// 购买基金
//- (void)buyFund{
//    [SVProgressHUD show];
//    NSMutableDictionary *fundInfo = [NSMutableDictionary dictionary];
//    fundInfo[@"fundCode"] = _dataDict[@"fundCode"];
//    fundInfo[@"currency"] = @"156";
//    fundInfo[@"investmentAmount"] = self.moneyTf.text;
//    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//    dict[@"accountNumber"] = kAcount;
//    dict[@"agreementConfirmed"] = @"0";
//    dict[@"investorPayId"] = self.bankDetail[@"investorPayId"];
//    dict[@"password"] = self.tradePassword;
//    dict[@"payMethod"] = @"0";
//    dict[@"purchaseFunds"] = [NSArray arrayWithObjects:fundInfo, nil];
//    dict[@"riskConfirmed"] = @"1";
//    NSString *jsonString = [self dictionaryToJson:dict];
//    NSDictionary *dictThree = @{
//                                KparametTwo:@"/public/transaction/buy-fund-transaction"
//                                };
//    [HttpRequest sendPostRequestWithURL:nil WithParamets:dictThree WithJsonString:jsonString success:^(id result) {
//
//        NSLog(@"%@",result);
//
//        if ([result[@"code"] isEqualToString:@"0000"]) {
//            [SVProgressHUD showSuccessWithStatus:@"购买成功"];
//            [SVProgressHUD dismissWithDelay:1.0];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self.navigationController popViewControllerAnimated:YES];
//            });
//        }else{
//            [SVProgressHUD showInfoWithStatus:result[@"message"]];
//            [SVProgressHUD dismissWithDelay:1.5];
//        }
//    } failure:^(NSError *errer) {
//        [SVProgressHUD showErrorWithStatus:@"服务器错误，购买失败"];
//        [SVProgressHUD dismissWithDelay:1.0];
//    }];
//}

//
//- (void)getDataOne{
//    [SVProgressHUD show];
//    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
//    //    paramets[@"fundCodes"] = @"";
//    paramets[@"isBuyEnable"] = @"0";
//    //    paramets[@"isQDII"] = @"";
//    //    paramets[@"isMMFund"] = @"1";
//    //    paramets[@"isRecommended"] = @"";
//    paramets[@"sort"] = @"DESC";
//    paramets[@"period"] = @"year1";
//    //    paramets[@"geographicalSector"] = @"";
//    //    paramets[@"specializeSector"] = @"";
//    //    paramets[@"fundType"] = @"";
//    //    paramets[@"shortName"] = @"";
//    //    paramets[@"fundHouseCode"] = @"";
//    if (self.searchKey.length > 0) {
//        paramets[@"search"] = self.searchKey;
//    }
//    paramets[@"pageSize"] = @"10";
//    paramets[@"pageIndex"] = [NSString stringWithFormat:@"%ld", self.page];
//    NSString *jsonStr = [self dictionaryToJson:paramets];
//    NSDictionary *paramet = @{@"paramList":jsonStr,
//                              @"methodPath":KFundList};
//
//    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
//        NSString *code = result[@"code"];
//        if ([code isEqualToString:@"0000"]) {
//            [SVProgressHUD dismiss];
//            NSDictionary *dataDict = result[@"data"];
//            [self.fundArray addObjectsFromArray:dataDict[@"data"]];
//            NSArray *array = dataDict[@"data"];
//            if (array.count < 10) {
//                [self.fundList.mj_footer endRefreshingWithNoMoreData];
//            }else{
//                [self.fundList.mj_footer endRefreshing];
//            }
//            [self.fundList reloadData];
//            [self.fundList setContentOffset:CGPointMake(0, 0) animated:NO];
//        }else{
//            [SVProgressHUD showInfoWithStatus:result[@"message"]];
//            [SVProgressHUD dismissWithDelay:1.0];
//        }
//
//    } failure:^(NSError *errer) {
//        [SVProgressHUD showErrorWithStatus:@"网络错误"];
//        [SVProgressHUD dismissWithDelay:1.0];
//    }];
//}
#pragma mark -
#pragma mark 点击事件
-(void)chooseAllBtnClick:(UIButton*)btn
{
    [self.haveSelectArray removeAllObjects];
    btn.selected=!btn.selected;
    if (btn.selected==YES) {
        
        [btn setImage:[UIImage imageNamed:@"xuanzhong_sel"] forState:UIControlStateNormal];
        for (jijinAndGroupDetailModel*mode in self.jijinDataArray) {
            mode.isChoose=YES;
            [self.haveSelectArray addObject:mode];
            
        }
        
//        for (int i = 0; i < self.jijinDataArray.count; i++) {
//            [selectIndexArr addObject:[NSString stringWithFormat:@"%d",i]];
//        }
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"xuanzhong_dis"] forState:UIControlStateNormal];
        for (jijinAndGroupDetailModel*mode in self.jijinDataArray) {
            mode.isChoose=NO;
        }
//        [selectIndexArr removeAllObjects];
    }
    
    [_tableView reloadData];
}
-(void)deleteBtnClick:(UIButton*)btn
{
    if (self.haveSelectArray.count <= 0) {
        return;
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"确定从购物车中删除？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteShop];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
//点击购物车购买
-(void)buyBtnClick:(UIButton*)btn
{
//    LLLog(@"%@",selectIndexArr);
//    LLLog(@"%@",investMoneyArr);
    if (self.haveSelectArray.count <= 0) {
        return;
    }
    //判断投资金额是否为空
    
//    for (int i = 0; i < selectIndexArr.count; i++) {
//        NSString * amount = investMoneyArr[i];
//        if ([amount integerValue] <= 0) {
//            [SVProgressHUD showInfoWithStatus:@"请输入投资基金"];
//            [SVProgressHUD dismissWithDelay:1.0];
//            return;
//        }
//    }
    
    for (int i = 0; i < self.haveSelectArray.count; i++) {
        jijinAndGroupDetailModel * model = self.haveSelectArray[i];
        if ([model.Amount integerValue] <= 0) {
            [SVProgressHUD showInfoWithStatus:@"请输入投资基金"];
            [SVProgressHUD dismissWithDelay:1.0];
            return;
        }
    }
    ShoppingBuyViewController * buyVC = [[ShoppingBuyViewController alloc]init];
    buyVC.isShop = YES;
    buyVC.shopArray = self.haveSelectArray;
    
    [buyVC setBlock:^(NSArray *shopArray) {
        shopArrays = [[NSMutableArray alloc]init];
        [shopArrays addObjectsFromArray:shopArray];
    }];
    [self.navigationController pushViewController:buyVC animated:YES];
}
-(UITableView*)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-50-KNavHeight-49)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.rowHeight=120;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor=ZKcolor(240, 240, 240);
        [_tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
        [self.view addSubview:_tableView];
       
    }
    return _tableView;
}
-(UIView*)kongbaiView
{
    if (!_kongbaiView) {
        _kongbaiView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
        _kongbaiView.backgroundColor=[UIColor whiteColor];
        
        
        UIImage*image=[UIImage imageNamed:@"bbgg.png"];
        UIImageView*kongImageView=[[UIImageView alloc]initWithFrame:CGRectMake(screenWidth/2-image.size.width/2, 50, image.size.width, image.size.height)];
        kongImageView.image=image;
        [_kongbaiView addSubview:kongImageView];
        
        
        UIButton*addJijin=[UIButton buttonWithType:UIButtonTypeCustom];
        addJijin.frame=CGRectMake(screenWidth/2-75, kongImageView.bottom+80, 150, 50);
        [addJijin setTitle:@"添加基金" forState:UIControlStateNormal];
        [addJijin setTitleColor:ZKcolor(255, 42, 0) forState:UIControlStateNormal];
        addJijin.layer.cornerRadius=25;
        [addJijin addTarget:self action:@selector(addjijinBtnClick) forControlEvents:UIControlEventTouchUpInside];
        addJijin.layer.borderColor=ZKcolor(255, 42, 0).CGColor;
        addJijin.layer.borderWidth=0.8;
        [_kongbaiView addSubview:addJijin];
    }
    return _kongbaiView;
}
-(void)addjijinBtnClick
{
    //self.tabBarController.selectedIndex=1;
    HDCFFundListVc * fundListVC = [HDCFFundListVc new];
    [self.navigationController pushViewController:fundListVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark UITableViewdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.jijinDataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ShoppingCarTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ShoppingCarTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ShoppingCarTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShoppingCarTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
       
    }
    
    jijinAndGroupDetailModel*model=self.jijinDataArray[indexPath.row];
    
    if (model.isChoose==YES) {
        [cell.chooseBtn setBackgroundImage:[UIImage imageNamed:@"xuanzhong_sel"] forState:UIControlStateNormal];
        cell.chooseBtn.selected=YES;
    }
    else
    {
        [cell.chooseBtn setBackgroundImage:[UIImage imageNamed:@"xuanzhong_dis"] forState:UIControlStateNormal];
        cell.chooseBtn.selected=NO;
    }
    //基金名字
    cell.nameLable.text = [NSString stringWithFormat:@"%@",model.fundName];
    //基金代码跟类型
    if (fundTypeArr.count <=0) {
        cell.styleLable.text = [NSString stringWithFormat:@"%@",model.fundCode];
    }
    else
    {
        NSArray * typeEArr = @[@"MM",@"BOND",@"MIXED",@"CP",@"EQ",@"AI",@"INDEX",@"ST",@"UNKNOWN"];
        NSArray * typeCArr = @[@"货币型",@"债券型",@"混合型",@"保本型",@"股票型",@"另类型",@"指数型",@"分级型",@"其他"];
        NSString * typeC;
        if (_jijinDataArray.count == fundTypeArr.count) {
            for (int i = 0;i < typeEArr.count;i++)
            {
                NSString * typeE = typeEArr[i];
                if ([typeE isEqualToString:fundTypeArr[indexPath.row]]) {
                    typeC = typeCArr[i];
                    break;
                }
            }
            cell.styleLable.text = [NSString stringWithFormat:@"%@   %@",model.fundCode,typeC];
        }
        else
        {
            cell.styleLable.text = [NSString stringWithFormat:@"%@",model.fundCode];
        }
    }
    
    if ([model.Amount intValue] > 0) {
        cell.amountTextField.text = [NSString stringWithFormat:@"%d",[model.Amount intValue]];
    }
    else
    {
        cell.amountTextField.text = [NSString stringWithFormat:@""];
    }
    
    cell.amountTextField.delegate = self;
    cell.amountTextField.tag = 100 + indexPath.row;
    
    cell.chooseBtn.tag=indexPath.row;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    [cell.chooseBtn addTarget:self action:@selector(chooseOneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)chooseOneBtnClick:(UIButton*)btn
{
    jijinAndGroupDetailModel*model=self.jijinDataArray[btn.tag];
    model.isChoose=!model.isChoose;
    btn.backgroundColor=[UIColor whiteColor];
    NSIndexPath*indepath=[NSIndexPath indexPathForRow:btn.tag inSection:0];
    [_tableView reloadRowsAtIndexPaths:@[indepath] withRowAnimation:UITableViewRowAnimationNone];
    
    if (model.isChoose==NO) {
        [self.deleteViewView.chooseBtn setImage:[UIImage imageNamed:@"xuanzhong_dis"] forState:UIControlStateNormal];
        self.deleteViewView.chooseBtn.selected=NO;
        [self.haveSelectArray removeObject:model];

//        [selectIndexArr removeObject:[NSString stringWithFormat:@"%ld",btn.tag]];

    }
    else
    {
        [self.haveSelectArray addObject:model];
//        [selectIndexArr addObject:[NSString stringWithFormat:@"%ld",btn.tag]];
    }
    
    if (self.haveSelectArray.count!=0) {
        self.deleteViewView.buyBtn.backgroundColor=COLOR_NAV;
        [self.deleteViewView.buyBtn setTitle:[NSString stringWithFormat:@"购买(%lu)",self.haveSelectArray.count]  forState:UIControlStateNormal];
    }
    else
    {
        self.deleteViewView.buyBtn.backgroundColor=[UIColor lightGrayColor];
        [self.deleteViewView.buyBtn setTitle:[NSString stringWithFormat:@"购买"]  forState:UIControlStateNormal];
    }
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
    for (int i = 0; i < self.jijinDataArray.count; i++) {
        if (textField.tag == 100 + i) {
            //LLLog(@"%d",i);
            indexs = i;
            textFieldStr = textField.text;
            [self editShop:self.jijinDataArray[i]];
        }
    }
}
#pragma mark -
#pragma mark 字典、数组转模型
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
-(NSString *)arrayToJSONString:(NSArray *)array
{
    NSError *error = nil;
    //    NSMutableArray *muArray = [NSMutableArray array];
    //    for (NSString *userId in array) {
    //        [muArray addObject:[NSString stringWithFormat:@"\"%@\"", userId]];
    //    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    //    NSString *jsonResult = [jsonTemp stringByReplacingOccurrencesOfString:@" " withString:@""];
    //    NSLog(@"json array is: %@", jsonResult);
    return jsonString;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
